﻿using System;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.ComponentModel.Design;
using Microsoft.Win32;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using EnvDTE80;
using EnvDTE;
using System.Linq;
using Microsoft.VisualStudio.ExtensionManager;
using FunctionPoint;
using FunctionPoint.Services;

namespace SiAAS.FunctionPointVSPackage
{
    [PackageRegistration(UseManagedResourcesOnly = true)]
    [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
    [ProvideMenuResource("Menus.ctmenu", 1)]
    [ProvideToolWindow(typeof(FunctionPointExplorerTreeControlHost))]
    [ProvideToolWindow(typeof(FunctionPointSelectorToolWindowHost))]
    [ProvideService(typeof(SFunctionPointManagerService))]
    [ProvideAutoLoad(VSConstants.UICONTEXT.SolutionExists_string)]
    [ProvideAutoLoad(VSConstants.UICONTEXT.CodeWindow_string)]
    [Guid(GuidList.guidFunctionPointVSPackagePkgString)]
    public sealed class FunctionPointVSPackagePackage : Package
    {


       
        // swithced to singleton because I was just wasting resources and causing lots of confusion.  
        // Think cJ... Think..
        private FunctionPointManagerService singletonServiceInstance = null;
        public FunctionPointVSPackagePackage()
        {
            Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}", this.ToString()));

            // http://msdn.microsoft.com/en-us/library/bb164693.aspx

            IServiceContainer serviceContainer = this as IServiceContainer;
            serviceContainer.AddService(typeof(SFunctionPointManagerService), new ServiceCreatorCallback((container, serviceType) =>
            {

                if (typeof(SFunctionPointManagerService) == serviceType)
                {
                    if (singletonServiceInstance == null)
                        singletonServiceInstance = new FunctionPointManagerService(this);

                    return singletonServiceInstance;
                }
                return null;
            }), true);


        }

        #region Tool window callbacks
        private void ShowFunctionPointExplorerToolWindow(object sender, EventArgs e)
        {
            // Get the instance number 0 of this tool window. This window is single instance so this instance
            // is actually the only one.
            // The last flag is set to true so that if the tool window does not exists it will be created.
            ToolWindowPane window = this.FindToolWindow(typeof(FunctionPointExplorerTreeControlHost), 0, true);
            if ((null == window) || (null == window.Frame))
            {
                throw new NotSupportedException(Resources.CanNotCreateWindow);
            }
            IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;



            Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(windowFrame.Show());
        }

        private void ShowFunctionPointSelectorToolWindow(object sender, EventArgs e)
        {
            ToolWindowPane window = this.FindToolWindow(typeof(FunctionPointSelectorToolWindowHost), 0, true);

            if ((null == window) || (null == window.Frame))
            {
                throw new NotSupportedException(Resources.CanNotCreateWindow);
            }
            IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
            FunctionPointSelectorToolWindowHost windowhost = (FunctionPointSelectorToolWindowHost)window;

            Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(windowFrame.Show());
        }
        #endregion

        #region Package Members

        /// <summary>
        /// Initialization of the package; this method is called right after the package is sited, so this is the place
        /// where you can put all the initilaization code  that rely on services provided by VisualStudio.
        /// </summary>
        protected override void Initialize()
        {
            Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString()));
            base.Initialize();

            // Add our command handlers for menu (commands must exist in the .vsct file)
            OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
            if (null != mcs)
            {
                // Create the command for the tool window
                CommandID toolwndCommandID = new CommandID(GuidList.guidFunctionPointVSPackageCmdSet, (int)PkgCmdIDList.cmdidFunctionPointExplorerToolWindow);
                MenuCommand menuToolWin = new MenuCommand(ShowFunctionPointExplorerToolWindow, toolwndCommandID);
                mcs.AddCommand(menuToolWin);

                // Command for the selector window (show multiple adornments for multiple function points on a single editor

                CommandID selectorToolwndCommandId = new CommandID(GuidList.guidFunctionPointVSPackageCmdSet, (int)PkgCmdIDList.cmdidFunctionPointSelectorToolWindow);
                MenuCommand menuSelectorToolWin = new MenuCommand(ShowFunctionPointSelectorToolWindow, selectorToolwndCommandId);
                mcs.AddCommand(menuSelectorToolWin);

            }


        }




        #endregion

    }
}
